# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=qscintilla
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-common")
pkgver=2.11.3
pkgrel=1
pkgdesc="A port to Qt5 of Neil Hodgson's Scintilla C++ editor class (mingw-w64)"
arch=('any')
license=("GPL")
url="https://riverbankcomputing.com/software/qscintilla/intro"
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2-pyqt5"
             "${MINGW_PACKAGE_PREFIX}-python3-pyqt5")
options=(!strip staticlibs !buildflags)
source=(https://www.riverbankcomputing.com/static/Downloads/QScintilla/${pkgver}/QScintilla-${pkgver}.tar.gz
        'run-command-via-sh.patch'
        'qmake-install-paths.patch'
        'designed-plugin-config-build.patch')
sha256sums=('cfadbb7f32fb8a9404fbe5db0ad14ac02229ebdf30a519141236080236ae2dd6'
            'a517b6427006c18db816dfcd087eee36e2f08c48c5147c48971f25118ea0c149'
            '517df6924d9d10104ee57372530692457cc3d4687d98ee6222f9764f0b31024b'
            'b9de93dc99426bebcb9516942b70b0cd8b17cba62de79becceb04737a1b0a2e7')

prepare() {
  cd QScintilla-${pkgver}
  patch -p1 -i "${srcdir}"/run-command-via-sh.patch
  patch -p1 -i "${srcdir}"/qmake-install-paths.patch
  patch -p1 -i "${srcdir}"/designed-plugin-config-build.patch

  cp -rf Python Python2
}

package_qscintilla() {
  pkgdesc="A port to Qt5 of Neil Hodgson's Scintilla C++ editor class"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${srcdir}/QScintilla-${pkgver}
  sed -e "s|%INSTALL_PREFIX%|$(cygpath -wm ${pkgdir})|g" \
    -i Qt4Qt5/qscintilla.pro \
    -i designer-Qt4Qt5/designer.pro

  cd Qt4Qt5
  ${MINGW_PREFIX}/bin/qmake CONFIG+=build_all qscintilla.pro
  make install

  cd ../designer-Qt4Qt5
  QMAKEFEATURES=../Qt4Qt5/features \
  ${MINGW_PREFIX}/bin/qmake CONFIG+=build_all INCLUDEPATH+=../Qt4Qt5 designer.pro
  make install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin/
}

package_python-qscintilla-common() {
  pkgdesc="Common python qscintilla bindings files shared between python3-qscintilla and python2-qscintilla"
  depends=("${MINGW_PACKAGE_PREFIX}-qscintilla")

  cd QScintilla-${pkgver}/Python
  MSYS2_ARG_CONV_EXCL="${MINGW_PREFIX}" \
  make DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install -j1

  # Provided by python-qscintilla
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib
}

package_python3-qscintilla() {
  pkgdesc="Python 3.x bindings for QScintilla2"
  depends=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common" "${MINGW_PACKAGE_PREFIX}-python3-pyqt5")

  cd QScintilla-${pkgver}/Python
  ${MINGW_PREFIX}/bin/python3 configure.py \
    --pyqt=PyQt5 \
    --qsci-featuresdir=../Qt4Qt5/features \
    --qsci-incdir=../Qt4Qt5 \
    --qsci-libdir=../Qt4Qt5/release

  make
  MSYS2_ARG_CONV_EXCL="${MINGW_PREFIX}" \
  make INSTALL_ROOT="${pkgdir}" install -j1

  rm -rf "${pkgdir}${MINGW_PREFIX}"/share
}

package_python2-qscintilla() {
  pkgdesc="Python 2.x bindings for QScintilla2"
  depends=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common" "${MINGW_PACKAGE_PREFIX}-python2-pyqt5")

  cd QScintilla-${pkgver}/Python2
  ${MINGW_PREFIX}/bin/python2 configure.py \
    --pyqt=PyQt5 \
    --qsci-featuresdir=../Qt4Qt5/features \
    --qsci-incdir=../Qt4Qt5 \
    --qsci-libdir=../Qt4Qt5/release

  make
  MSYS2_ARG_CONV_EXCL="${MINGW_PREFIX}" \
  make INSTALL_ROOT="${pkgdir}" install -j1

  rm -rf "${pkgdir}${MINGW_PREFIX}"/share
}

### Wrappers

package_mingw-w64-i686-qscintilla() {
  package_qscintilla
}

package_mingw-w64-i686-python-qscintilla-common() {
  package_python-qscintilla-common
}

package_mingw-w64-i686-python2-qscintilla() {
  package_python2-qscintilla
}

package_mingw-w64-i686-python3-qscintilla() {
  package_python3-qscintilla
}

package_mingw-w64-x86_64-qscintilla() {
  package_qscintilla
}

package_mingw-w64-x86_64-python-qscintilla-common() {
  package_python-qscintilla-common
}

package_mingw-w64-x86_64-python2-qscintilla() {
  package_python2-qscintilla
}

package_mingw-w64-x86_64-python3-qscintilla() {
  package_python3-qscintilla
}
